package com.mms.member.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mms.common.controller.BaseController;
import com.mms.common.utils.MD5Utils;
import com.mms.common.utils.PageUtils;
import com.mms.common.utils.Query;
import com.mms.common.utils.R;
import com.mms.member.domain.MemberOrderDo;
import com.mms.member.service.MemberOrderService;
import com.mms.system.domain.UserDO;
import com.mms.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
@RequestMapping("/member/order")
@Controller
@Slf4j
public class MemberController extends BaseController {
	
	private String prefix="member/order"  ;
	
	@Autowired
	MemberOrderService memberOrderService;

	@Autowired
	UserService userService;
	
	@GetMapping("/toList")
	@RequiresPermissions("member:order")
	String toList() {
		return prefix+"/list";
	}
	
	
	/**
	 * 列表
	 * @param params
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("member:order")
	@ResponseBody
	PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<MemberOrderDo> memberOrderList = memberOrderService.list(query);
		int total = memberOrderService.count(query);
		PageUtils pageUtil = new PageUtils(memberOrderList, total);
		return pageUtil;
	}
	
	@GetMapping("/toProverOrder/{id}")
	@RequiresPermissions("member:order")
	String toProverOrder(@PathVariable("id") Long id, Model model) {
		MemberOrderDo memberOrderDo = memberOrderService.get(id);
		//MemberUser memberUser = memberUserService.get(memberOrderDo.getMemberNumber());
		//model.addAttribute("memberUser",memberUser);
		model.addAttribute("memberOrder", memberOrderDo);
		return prefix+"/proverOrder";
	}
	
	@GetMapping("/viewOrder/{id}")
	@RequiresPermissions("member:order")
	String viewOrder(@PathVariable("id") Long id, Model model) {
		MemberOrderDo memberOrderDo = memberOrderService.get(id);
		//MemberUser memberUser = memberUserService.get(memberOrderDo.getMemberNumber());
		//model.addAttribute("memberUser",memberUser);
		model.addAttribute("memberOrder", memberOrderDo);
		return prefix+"/viewOrder";
	}
	
	/**
	 * 审批
	 */
	@ResponseBody
	@RequestMapping("/proverOrder")
	@RequiresPermissions("member:order")
	public R proverOrder(MemberOrderDo memberOrderDo) {
		if("S".equals(memberOrderDo.getOrderStatus())) { //审核通过
			//会员信息激活
			//获取魔板用户 直接赋值
			//新建登陆用户并赋值特定较色
			UserDO userDO = null;
			if("SD".equals(memberOrderDo.getOrderType())) {
				 userDO = userService.get(999L);
			}else if("CC".equals(memberOrderDo.getOrderType())) {
				 userDO = userService.get(1000L);
			}
			userDO.setStatus(1);
			userDO.setUsername(memberOrderDo.getMemberNumber());
			userDO.setName(memberOrderDo.getApplyName());
			userDO.setPassword(MD5Utils.encrypt(userDO.getUsername(), "123456"));
			userService.save(userDO);
		}
		memberOrderService.update(memberOrderDo);
		return R.ok();
	}
	
	

	@GetMapping()
	String portal() {
		log.info("===========");
		return "member/member_register";
	}
	
}
